package cn.itsource.blog.controller;

import cn.itsource.annotation.PreAuthorize;
import cn.itsource.blog.domain.Article;
import cn.itsource.blog.dto.ArticleDto;
import cn.itsource.blog.query.ArticleQuery;
import cn.itsource.blog.service.IArticleService;
import cn.itsource.util.JsonResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：文章Article
 */
@RestController
@RequestMapping("/article")
@Api(tags = "文章接口类")
public class ArticleController {
    @Autowired //注入，从spring容器中根据名字去查询对象
    private IArticleService articleService;

    /**
     * 查询所有
     * @return
     */
    @ApiOperation(value = "查询所有")
    @GetMapping
    @PreAuthorize(name = "查询所有文章",sn = "article:getAll",descs = "查询所有文章")
     List<Article> findAll(){
        return articleService.findAll();
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @PreAuthorize(name = "查询单个文章",sn = "article:getOne",descs = "查询单个文章")
     Article findOne(@PathVariable("id")Long id){
        return articleService.findOne(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除文章",sn = "article:delete",descs = "删除文章")
     JsonResult delete(@PathVariable("id")Long id){
        try {
            articleService.delete(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 添加或修改
     * @param article
     * @return
     */
    @PutMapping
     JsonResult addOrUpdate(@RequestBody Article article){
        try {
           if(article.getId()==null){
               articleService.add(article);
           }else{
               articleService.update(article);
           }
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
     JsonResult patchDel(@RequestBody Long[] ids){
        try {
            articleService.patchDel(ids);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 分页查询+高级查询
     * @param articleQuery
     * @return
     */
    @PostMapping
     PageInfo<Article> queryPage(@RequestBody ArticleQuery articleQuery){
        return articleService.queryPage(articleQuery);
    }

    @GetMapping("/countByArticleType")
     List<ArticleDto> countByArticleType(){
        return articleService.countByArticleType();
    }

    @GetMapping("/countByTime")
     List<ArticleDto> countByTime(){
        return articleService.countByTime();
    }


    @GetMapping("/dependencyArticle/{id}")
     List<Article> dependencyArticle(@PathVariable("id")Long id){
        return articleService.dependencyArticle(id);
    }


}
